package com.example.coreshopapp.task;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.example.coreshopapp.app.Constants;
import com.example.coreshopapp.bean.LoginData;
import com.example.coreshopapp.bean.LoginReq;
import com.example.coreshopapp.bean.SysuserData;
import com.example.coreshopapp.http.AccessNet;
import com.google.gson.Gson;

public class LoginTask {
    // 处理数据文件
    private String userName;
    private String userPassword;//多关键字查询
    private String msg = "操作失败";
    private Handler outHandler;
    private Handler outUserHandler;

    public void setOutHandler(Handler outHandler) {
        this.outHandler = outHandler;
    }
    public void setUserHandler(Handler userHandler) {
        this.outUserHandler = userHandler;
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        //        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Message message = outHandler.obtainMessage();
            switch (msg.what) {
                case 9900:
                    message.what = 9900;
                    message.obj = msg.obj;// info;
                    LoginData obj = (LoginData) msg.obj;
                    Constants.AppConstVars.token = obj.getToken().getToken();
//                    sysUser();
                    outHandler.sendMessage(message);
                    break;
                case 9901:
                    message.what = 9901;
                    message.obj = msg.obj;// info;
                    Log.d("用户登录50010,", msg.obj.toString() + "");
                    outHandler.sendMessage(message);
                    Log.d("9901", msg.obj.toString());
                    break;
                case 9902:
                    Log.d("9902",  msg.obj.toString());
                    message.what = 9902;
                    message.obj = "操作异常，请重试";// info;
                    outHandler.sendMessage(message);
                    break;
                case 9904:
                    message.what = 9904;
                    message.obj = msg.obj;// info;
                    outHandler.sendMessage(message);
                    break;
            }
        }
    };

    public LoginTask(String userName, String userPassword) {
        this.userName = userName;
        this.userPassword = userPassword;
    }

    private boolean freshNetData() {
        // 登录参数
        new Thread(() -> {
            Gson gson = new Gson();
            LoginReq postDto = new LoginReq();
            postDto.setUsername(userName);
            postDto.setPassword(userPassword);
            postDto.setAuthCode("TrLyAuth3987@Super");
            Log.d("用户登录gson,", gson.toJson(postDto));
            boolean re = AccessNet.loginByPos(gson.toJson(postDto), mHandler, 6); // 调用接口
        }).start();
        return true;
    }

    public Boolean execute() {
        return freshNetData();
    }


    @SuppressLint("HandlerLeak")
    private Handler userHandler = new Handler() {
        //        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Message message = outUserHandler.obtainMessage();
            switch (msg.what) {
                case 9900:
                    message.what = 9900;
                    message.obj = msg.obj;// info;
                    Constants.AppConstVars.sysUser = JSON.parseObject(msg.obj.toString(), SysuserData.class);
                    outUserHandler.sendMessage(message);
                    break;
                case 9901:
                    message.what = 9901;
                    message.obj = msg.obj;// info;
                    outUserHandler.sendMessage(message);
                    break;
                case 9902:
                    message.what = 9902;
                    message.obj = "操作异常，请重试";// info;
                    outUserHandler.sendMessage(message);
                    break;
                case 9904:
                    message.what = 9904;
                    message.obj = msg.obj;// info;
                    outUserHandler.sendMessage(message);
                    break;
            }
        }
    };

    private boolean sysUser() {
        new Thread(() -> {
            boolean b = AccessNet.postDataNet(userHandler, Constants.urlClass.Login_User, 6, "");
        }).start();
        return true;
    }
    public Boolean userExecute() {
        return sysUser();
    }

}
